{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "5023121a76eacab6"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 3、练习series和df的索引操作,函数应用",
   "id": "69db667aae9e313a"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Series",
   "id": "bb68f4dfff44c82a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:48:38.222044Z",
     "start_time": "2025-01-08T11:48:37.958824Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "# 生成一个Series\n",
    "\n",
    "ser_obj = pd.Series(range(10, 20)) #默认索引是0-9\n",
    "print(ser_obj) #打印输出会带有类型\n"
   ],
   "id": "a9dec01ecad83f08",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:48:55.130808Z",
     "start_time": "2025-01-08T11:48:55.121847Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('-'*50)\n",
    "# 获取数据\n",
    "print(ser_obj.values)  #values实际是ndarray\n",
    "print(type(ser_obj.values)) #类型是ndarray\n",
    "# 获取索引\n",
    "print(ser_obj.index)  #内部自带的类型--RangeIndex\n",
    "ser_obj.dtype #数据类型"
   ],
   "id": "ca9bfd0f69e3eef8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "[10 11 12 13 14 15 16 17 18 19]\n",
      "<class 'numpy.ndarray'>\n",
      "RangeIndex(start=0, stop=10, step=1)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:49:25.510475Z",
     "start_time": "2025-01-08T11:49:25.505238Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj[0]) \n",
    "ser_obj[9] \n",
    "# 访问不存在的索引下标会报keyerror"
   ],
   "id": "4f316941f27b54f2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.int64(19)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:49:44.758472Z",
     "start_time": "2025-01-08T11:49:44.731839Z"
    }
   },
   "cell_type": "code",
   "source": "ser_obj[10] # 访问不存在的索引下标会报keyerror",
   "id": "b89a88e6418a51c9",
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "10",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "File \u001B[1;32mC:\\Python\\py\\Lib\\site-packages\\pandas\\core\\indexes\\range.py:413\u001B[0m, in \u001B[0;36mRangeIndex.get_loc\u001B[1;34m(self, key)\u001B[0m\n\u001B[0;32m    412\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m--> 413\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_range\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mindex\u001B[49m\u001B[43m(\u001B[49m\u001B[43mnew_key\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    414\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m err:\n",
      "\u001B[1;31mValueError\u001B[0m: 10 is not in range",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001B[1;31mKeyError\u001B[0m                                  Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[6], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mser_obj\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m10\u001B[39;49m\u001B[43m]\u001B[49m \u001B[38;5;66;03m#\u001B[39;00m\n",
      "File \u001B[1;32mC:\\Python\\py\\Lib\\site-packages\\pandas\\core\\series.py:1121\u001B[0m, in \u001B[0;36mSeries.__getitem__\u001B[1;34m(self, key)\u001B[0m\n\u001B[0;32m   1118\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_values[key]\n\u001B[0;32m   1120\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m key_is_scalar:\n\u001B[1;32m-> 1121\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_get_value\u001B[49m\u001B[43m(\u001B[49m\u001B[43mkey\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m   1123\u001B[0m \u001B[38;5;66;03m# Convert generator to list before going through hashable part\u001B[39;00m\n\u001B[0;32m   1124\u001B[0m \u001B[38;5;66;03m# (We will iterate through the generator there to check for slices)\u001B[39;00m\n\u001B[0;32m   1125\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m is_iterator(key):\n",
      "File \u001B[1;32mC:\\Python\\py\\Lib\\site-packages\\pandas\\core\\series.py:1237\u001B[0m, in \u001B[0;36mSeries._get_value\u001B[1;34m(self, label, takeable)\u001B[0m\n\u001B[0;32m   1234\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_values[label]\n\u001B[0;32m   1236\u001B[0m \u001B[38;5;66;03m# Similar to Index.get_value, but we do not fall back to positional\u001B[39;00m\n\u001B[1;32m-> 1237\u001B[0m loc \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mindex\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget_loc\u001B[49m\u001B[43m(\u001B[49m\u001B[43mlabel\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m   1239\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m is_integer(loc):\n\u001B[0;32m   1240\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_values[loc]\n",
      "File \u001B[1;32mC:\\Python\\py\\Lib\\site-packages\\pandas\\core\\indexes\\range.py:415\u001B[0m, in \u001B[0;36mRangeIndex.get_loc\u001B[1;34m(self, key)\u001B[0m\n\u001B[0;32m    413\u001B[0m         \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_range\u001B[38;5;241m.\u001B[39mindex(new_key)\n\u001B[0;32m    414\u001B[0m     \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m err:\n\u001B[1;32m--> 415\u001B[0m         \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mKeyError\u001B[39;00m(key) \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01merr\u001B[39;00m\n\u001B[0;32m    416\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(key, Hashable):\n\u001B[0;32m    417\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mKeyError\u001B[39;00m(key)\n",
      "\u001B[1;31mKeyError\u001B[0m: 10"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:49:59.074316Z",
     "start_time": "2025-01-08T11:49:59.067805Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj * 2)  #元素级乘法\n",
    "print(ser_obj > 15) #返回一个bool序列,满足条件为TRUE"
   ],
   "id": "36a1a92ed0317078",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    20\n",
      "1    22\n",
      "2    24\n",
      "3    26\n",
      "4    28\n",
      "5    30\n",
      "6    32\n",
      "7    34\n",
      "8    36\n",
      "9    38\n",
      "dtype: int64\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6     True\n",
      "7     True\n",
      "8     True\n",
      "9     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:51:54.644404Z",
     "start_time": "2025-01-08T11:51:54.637279Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#字典变为series，索引是字典的key，value是字典的value，感受非默认索引\n",
    "\n",
    "year_data = {2001: 17.8, 2005: 20.1, 2003: 16.5}\n",
    "ser_obj2 = pd.Series(year_data)\n",
    "print(ser_obj2)\n",
    "print('-'*50)\n",
    "print(ser_obj2.index)\n",
    "print('-'*50)\n",
    "print(ser_obj2[2001])\n",
    "ser_obj2.values"
   ],
   "id": "5c58f73ed722604c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001    17.8\n",
      "2005    20.1\n",
      "2003    16.5\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "Index([2001, 2005, 2003], dtype='int64')\n",
      "--------------------------------------------------\n",
      "17.8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([17.8, 20.1, 16.5])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:51:55.760430Z",
     "start_time": "2025-01-08T11:51:55.754242Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#有点鸡肋\n",
    "print(ser_obj2.name) #Series名字，默认为none\n",
    "print(ser_obj2.index.name)  #索引名字，默认为none\n",
    "ser_obj2.name = 'temp'\n",
    "ser_obj2.index.name = 'year1'\n",
    "print(ser_obj2.name) #Series名字，默认为none\n",
    "print(ser_obj2.index.name)  #索引名字，默认为none\n",
    "print('-'*50)\n",
    "print(ser_obj2.head())  #head默认显示前5行"
   ],
   "id": "6ad54ee9dce4305f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "None\n",
      "temp\n",
      "year1\n",
      "--------------------------------------------------\n",
      "year1\n",
      "2001    17.8\n",
      "2005    20.1\n",
      "2003    16.5\n",
      "Name: temp, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Dataframe",
   "id": "8064350dd5d634a0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:52:49.664487Z",
     "start_time": "2025-01-08T11:52:49.657297Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 通过ndarray构建DataFrame(二维数组）\n",
    "t = pd.DataFrame(np.arange(12).reshape((3,4))) #默认索引是0-9\n",
    "print(t)\n",
    "print('-'*50)"
   ],
   "id": "31f197710a38693e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1   2   3\n",
      "0  0  1   2   3\n",
      "1  4  5   6   7\n",
      "2  8  9  10  11\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:54:59.816458Z",
     "start_time": "2025-01-08T11:54:59.808458Z"
    }
   },
   "cell_type": "code",
   "source": [
    "array = np.random.randn(5,4) #np.random.randn生成0-1的(5,4)数组\n",
    "print(array)\n",
    "print('-'*50)\n",
    "df_obj = pd.DataFrame(array)\n",
    "print(df_obj.head()) #df_obj.head()默认显示前5行"
   ],
   "id": "4cfc5ce286219ef0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.82893628 -0.59399343 -0.58596     1.9203494 ]\n",
      " [ 0.46369886 -0.53633619  0.12263845 -0.08573604]\n",
      " [-0.35937412 -0.2209335  -0.30316582  2.15973005]\n",
      " [ 0.57087937  0.00818674 -0.77211365 -0.79958901]\n",
      " [ 1.0771693   0.42427005 -0.14701238  1.09717782]]\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.828936 -0.593993 -0.585960  1.920349\n",
      "1  0.463699 -0.536336  0.122638 -0.085736\n",
      "2 -0.359374 -0.220933 -0.303166  2.159730\n",
      "3  0.570879  0.008187 -0.772114 -0.799589\n",
      "4  1.077169  0.424270 -0.147012  1.097178\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:56:07.740088Z",
     "start_time": "2025-01-08T11:56:07.734176Z"
    }
   },
   "cell_type": "code",
   "source": "t.loc[0] #单独把某一行取出来,类型是series，默认为行",
   "id": "c7cbcc2b2c27d6d5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "Name: 0, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "loc对标签索引，iloc对下标索引",
   "id": "3ef83daddbad52db"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:56:33.331751Z",
     "start_time": "2025-01-08T11:56:33.327215Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列表套字典  变df\n",
    "d2 =[{\"name\" : \"xiaohong\" ,\"age\" :32,\"tel\" :10010},\n",
    "     { \"name\": \"xiaogang\" ,\"tel\": 10000} ,\n",
    "     {\"name\":\"xiaowang\" ,\"age\":22}]\n",
    "df6=pd.DataFrame(d2) #字典的key变成列的索引\n",
    "print(df6) #缺失值会用NaN填充\n",
    "print(type(df6.values)) #ndarray"
   ],
   "id": "df2dd58f33e77c96",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name   age      tel\n",
      "0  xiaohong  32.0  10010.0\n",
      "1  xiaogang   NaN  10000.0\n",
      "2  xiaowang  22.0      NaN\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:57:07.667345Z",
     "start_time": "2025-01-08T11:57:07.662432Z"
    }
   },
   "cell_type": "code",
   "source": "pd.Series(1, index=list(range(3, 7)), dtype='float32')    #数值都为1，索引为3-6，数值类型为float32",
   "id": "6095be1183139be0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3    1.0\n",
       "4    1.0\n",
       "5    1.0\n",
       "6    1.0\n",
       "dtype: float32"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:58:30.180599Z",
     "start_time": "2025-01-08T11:58:30.173413Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#df中不同列可以是不同的数据类型,同一列必须是一个数据类型\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2)"
   ],
   "id": "ab3dcfa3fb55d730",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:59:49.080504Z",
     "start_time": "2025-01-08T11:59:48.851521Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "dict_data = {'A': [1,2],                #要么个数为1，要么个数与最长保持一致\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2)"
   ],
   "id": "d7d86296b5dbff6d",
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "All arrays must be of the same length",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[24], line 9\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mnumpy\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m \u001B[38;5;21;01mnp\u001B[39;00m\n\u001B[0;32m      3\u001B[0m dict_data \u001B[38;5;241m=\u001B[39m {\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mA\u001B[39m\u001B[38;5;124m'\u001B[39m: [\u001B[38;5;241m1\u001B[39m,\u001B[38;5;241m2\u001B[39m],\n\u001B[0;32m      4\u001B[0m              \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mB\u001B[39m\u001B[38;5;124m'\u001B[39m: pd\u001B[38;5;241m.\u001B[39mTimestamp(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m20190926\u001B[39m\u001B[38;5;124m'\u001B[39m),\n\u001B[0;32m      5\u001B[0m              \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mC\u001B[39m\u001B[38;5;124m'\u001B[39m: pd\u001B[38;5;241m.\u001B[39mSeries(\u001B[38;5;241m1\u001B[39m, index\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mlist\u001B[39m(\u001B[38;5;28mrange\u001B[39m(\u001B[38;5;241m4\u001B[39m)),dtype\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mfloat32\u001B[39m\u001B[38;5;124m'\u001B[39m),\n\u001B[0;32m      6\u001B[0m              \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mD\u001B[39m\u001B[38;5;124m'\u001B[39m: np\u001B[38;5;241m.\u001B[39marray([\u001B[38;5;241m1\u001B[39m,\u001B[38;5;241m2\u001B[39m,\u001B[38;5;241m3\u001B[39m,\u001B[38;5;241m4\u001B[39m],dtype\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mint32\u001B[39m\u001B[38;5;124m'\u001B[39m),\n\u001B[0;32m      7\u001B[0m              \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mE\u001B[39m\u001B[38;5;124m'\u001B[39m: [\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mPython\u001B[39m\u001B[38;5;124m\"\u001B[39m,\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mJava\u001B[39m\u001B[38;5;124m\"\u001B[39m,\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mC++\u001B[39m\u001B[38;5;124m\"\u001B[39m,\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mC\u001B[39m\u001B[38;5;124m\"\u001B[39m],\n\u001B[0;32m      8\u001B[0m              \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mF\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mwangdao\u001B[39m\u001B[38;5;124m'\u001B[39m }\n\u001B[1;32m----> 9\u001B[0m df_obj2 \u001B[38;5;241m=\u001B[39m \u001B[43mpd\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mDataFrame\u001B[49m\u001B[43m(\u001B[49m\u001B[43mdict_data\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m     10\u001B[0m \u001B[38;5;28mprint\u001B[39m(df_obj2)\n",
      "File \u001B[1;32mC:\\Python\\py\\Lib\\site-packages\\pandas\\core\\frame.py:778\u001B[0m, in \u001B[0;36mDataFrame.__init__\u001B[1;34m(self, data, index, columns, dtype, copy)\u001B[0m\n\u001B[0;32m    772\u001B[0m     mgr \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_init_mgr(\n\u001B[0;32m    773\u001B[0m         data, axes\u001B[38;5;241m=\u001B[39m{\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mindex\u001B[39m\u001B[38;5;124m\"\u001B[39m: index, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mcolumns\u001B[39m\u001B[38;5;124m\"\u001B[39m: columns}, dtype\u001B[38;5;241m=\u001B[39mdtype, copy\u001B[38;5;241m=\u001B[39mcopy\n\u001B[0;32m    774\u001B[0m     )\n\u001B[0;32m    776\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(data, \u001B[38;5;28mdict\u001B[39m):\n\u001B[0;32m    777\u001B[0m     \u001B[38;5;66;03m# GH#38939 de facto copy defaults to False only in non-dict cases\u001B[39;00m\n\u001B[1;32m--> 778\u001B[0m     mgr \u001B[38;5;241m=\u001B[39m \u001B[43mdict_to_mgr\u001B[49m\u001B[43m(\u001B[49m\u001B[43mdata\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mindex\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mcolumns\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mdtype\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mdtype\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mcopy\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mcopy\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtyp\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmanager\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    779\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(data, ma\u001B[38;5;241m.\u001B[39mMaskedArray):\n\u001B[0;32m    780\u001B[0m     \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mnumpy\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mma\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m mrecords\n",
      "File \u001B[1;32mC:\\Python\\py\\Lib\\site-packages\\pandas\\core\\internals\\construction.py:503\u001B[0m, in \u001B[0;36mdict_to_mgr\u001B[1;34m(data, index, columns, dtype, typ, copy)\u001B[0m\n\u001B[0;32m    499\u001B[0m     \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m    500\u001B[0m         \u001B[38;5;66;03m# dtype check to exclude e.g. range objects, scalars\u001B[39;00m\n\u001B[0;32m    501\u001B[0m         arrays \u001B[38;5;241m=\u001B[39m [x\u001B[38;5;241m.\u001B[39mcopy() \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mhasattr\u001B[39m(x, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mdtype\u001B[39m\u001B[38;5;124m\"\u001B[39m) \u001B[38;5;28;01melse\u001B[39;00m x \u001B[38;5;28;01mfor\u001B[39;00m x \u001B[38;5;129;01min\u001B[39;00m arrays]\n\u001B[1;32m--> 503\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43marrays_to_mgr\u001B[49m\u001B[43m(\u001B[49m\u001B[43marrays\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mcolumns\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mindex\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mdtype\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mdtype\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtyp\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtyp\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mconsolidate\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mcopy\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32mC:\\Python\\py\\Lib\\site-packages\\pandas\\core\\internals\\construction.py:114\u001B[0m, in \u001B[0;36marrays_to_mgr\u001B[1;34m(arrays, columns, index, dtype, verify_integrity, typ, consolidate)\u001B[0m\n\u001B[0;32m    111\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m verify_integrity:\n\u001B[0;32m    112\u001B[0m     \u001B[38;5;66;03m# figure out the index, if necessary\u001B[39;00m\n\u001B[0;32m    113\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m index \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[1;32m--> 114\u001B[0m         index \u001B[38;5;241m=\u001B[39m \u001B[43m_extract_index\u001B[49m\u001B[43m(\u001B[49m\u001B[43marrays\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    115\u001B[0m     \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m    116\u001B[0m         index \u001B[38;5;241m=\u001B[39m ensure_index(index)\n",
      "File \u001B[1;32mC:\\Python\\py\\Lib\\site-packages\\pandas\\core\\internals\\construction.py:677\u001B[0m, in \u001B[0;36m_extract_index\u001B[1;34m(data)\u001B[0m\n\u001B[0;32m    675\u001B[0m lengths \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mlist\u001B[39m(\u001B[38;5;28mset\u001B[39m(raw_lengths))\n\u001B[0;32m    676\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mlen\u001B[39m(lengths) \u001B[38;5;241m>\u001B[39m \u001B[38;5;241m1\u001B[39m:\n\u001B[1;32m--> 677\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mAll arrays must be of the same length\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[0;32m    679\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m have_dicts:\n\u001B[0;32m    680\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\n\u001B[0;32m    681\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mMixing dicts with non-Series may lead to ambiguous ordering.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m    682\u001B[0m     )\n",
      "\u001B[1;31mValueError\u001B[0m: All arrays must be of the same length"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:00:32.670764Z",
     "start_time": "2025-01-08T12:00:32.665982Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('-'*50)\n",
    "print(df_obj2.index) #行索引,重点\n",
    "#补课改变\n",
    "# df_obj2.index[0]=2  不可以单独修改某个索引值\n",
    "print(df_obj2.columns) #列索引，重点\n",
    "df_obj2.dtypes #每一列的数据类型，重点"
   ],
   "id": "3070f15d0b75ab0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "Index([0, 1, 2, 3], dtype='int64')\n",
      "Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "A           object\n",
       "B    datetime64[s]\n",
       "C          float32\n",
       "D            int32\n",
       "E           object\n",
       "F           object\n",
       "dtype: object"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:01:31.527524Z",
     "start_time": "2025-01-08T12:01:31.519956Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 感受日期,初始化df，设置行索引，列索引\n",
    "dates = pd.date_range('20130101', periods=6) #默认freq='D'，即天   #pd.date_range生成datetime64[ns]类型，periods=6即从开头开始有6天\n",
    "df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))\n",
    "print(df)\n",
    "print('-'*50)\n",
    "print(df.index)"
   ],
   "id": "e633dbd703b8b73b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   A         B         C         D\n",
      "2013-01-01  0.674833  1.006733 -0.164589  0.605883\n",
      "2013-01-02  1.301038 -0.674480  0.909196  0.152100\n",
      "2013-01-03 -0.241407  0.043177 -1.269014 -0.040641\n",
      "2013-01-04 -0.894389 -0.750763 -1.485170 -1.425375\n",
      "2013-01-05  0.154609  0.800950  0.515149  0.092507\n",
      "2013-01-06  0.135477  0.040730  0.200457  1.739377\n",
      "--------------------------------------------------\n",
      "DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',\n",
      "               '2013-01-05', '2013-01-06'],\n",
      "              dtype='datetime64[ns]', freq='D')\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:05:08.812858Z",
     "start_time": "2025-01-08T12:05:08.807917Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#取数据\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "print(type(df_obj2))\n",
    "print('-'*50)\n",
    "#pd中使用索引名来取某一行，或者列\n",
    "print(df_obj2['B'])\n",
    "print('-'*50)\n",
    "#把df的某一列取出来是series\n",
    "print(type(df_obj2['B']))"
   ],
   "id": "3514d732a2ef3bf7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  2 2019-09-26  1.0  2    Java  wangdao\n",
      "2  w 2019-09-26  1.0  3     C++  wangdao\n",
      "3  d 2019-09-26  1.0  4       C  wangdao\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "0   2019-09-26\n",
      "1   2019-09-26\n",
      "2   2019-09-26\n",
      "3   2019-09-26\n",
      "Name: B, dtype: datetime64[s]\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:05:25.792643Z",
     "start_time": "2025-01-08T12:05:25.787577Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#增加列数据，列名是自定义的\n",
    "df_obj2['G'] = df_obj2['D'] + 4\n",
    "print(df_obj2.head())"
   ],
   "id": "17b3baa45c6d336e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F  G\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao  5\n",
      "1  2 2019-09-26  1.0  2    Java  wangdao  6\n",
      "2  w 2019-09-26  1.0  3     C++  wangdao  7\n",
      "3  d 2019-09-26  1.0  4       C  wangdao  8\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:05:35.733134Z",
     "start_time": "2025-01-08T12:05:35.727898Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除列\n",
    "del(df_obj2['G'])\n",
    "print(df_obj2.head())"
   ],
   "id": "264c1186899090b9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  2 2019-09-26  1.0  2    Java  wangdao\n",
      "2  w 2019-09-26  1.0  3     C++  wangdao\n",
      "3  d 2019-09-26  1.0  4       C  wangdao\n"
     ]
    }
   ],
   "execution_count": 40
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
